// 路由管理
import {createRouter, createWebHistory} from "vue-router";
import Layout from "../page/Layout.vue";
import Login from "../page/Login.vue";
import Home from "../page/Home.vue";
import Person from "../page/Person.vue";
import Test1_Cors from "../page/test/Test1_Cors.vue";
import Test2_TeacherLoginNavigate from "../page/test/Test2_TeacherLoginNavigate.vue";
import Test3_StudentLoginNavigate from "../page/test/Test3_StudentLoginNavigate.vue";
import Test4_FacultyAdminLoginNavigate from "../page/test/Test4_FacultyAdminLoginNavigate.vue";
import SplitPaperMetadataManagement from "../page/faculty-admin/data-management/SplitPaperMetadataManagement.vue";
import PredictionByKeywords from "../page/faculty-admin/PredictionByKeywords.vue";
import ExtractContentManagement from "../page/faculty-admin/data-management/ExtractContentManagement.vue";
import FactorScoreManagement from "../page/faculty-admin/data-management/FactorScoreManagement.vue";
import TemplateManagement from "../page/faculty-admin/data-management/TemplateManagement.vue";
import Test5_SA112View from "@/page/test/Test5_SA112View.vue";
import StatisticAnalysis from "@/page/faculty-admin/statistic-analysis/StatisticAnalysis.vue";

//路由信息
const routes = [
    {
        path: '/',
        redirect: '/login', // 访问根路径时，重定向到 /login
    },
    {
        path: '/login',
        name: "Login",
        component:Login,
        meta: {requiresAuth: false}
    },
    {
        path: '/',
        component: Layout,
        meta: {requiresAuth: true},
        children: [
            {
                path: '/home',
                name: 'Home',
                component:Home // 首页
            },
            {
                path: '/person',
                name: 'Person',
                component:Person // 个人中心
            },
            {
                path: '/test1',
                name: 'Test1',
                component: Test1_Cors
            },
            {
                path: '/test2',
                name: 'Test2',
                component: Test2_TeacherLoginNavigate,
                children:[
                    {
                        path: 'TemplateList',
                        name: 'TemplateList',
                        component:()=> import('@/page/teacher/dataCheck/TemplateList.vue')
                    },
                    {
                        path: 'MetadataList',
                        name: 'MetadataList',
                        component:()=> import('@/page/teacher/dataCheck/MetadataList.vue')
                    },
                    {
                    path: 'ExtractContentList',
                    name: 'ExtractContentList',
                    component:()=> import('@/page/teacher/dataCheck/ExtractContentList.vue')
                    },
                    {
                        path: 'ScoreManage',
                        name: 'ScoreManage',
                        component:()=> import('@/page/teacher/dataCheck/ScoreManage.vue')
                    },
                    {

                        path: 'statisticalAndAnalysis',
                        name: 'statisticalAndAnalysis',
                        children:[
                            {
                                path: 'OneStudent',
                                name: 'OneStudent',
                                component:()=> import('@/page/teacher/statisticalAndAnalysis/OneStudent.vue')
                            },
                            {
                                path: 'AllStudent',
                                name: 'AllStudent',
                                component:()=> import('@/page/teacher/statisticalAndAnalysis/AllStudent.vue')
                            }
                        ]
                    }


                ]

            },
            // {
            //     path: '/ExtractContentList',
            //     name: 'ExtractContentList',
            //     component:()=> import('@/page/teacher/ExtractContentList.vue')
            // },
            {
                path: '/test3',
                name: 'Test3',
                component: Test3_StudentLoginNavigate
            },
            {
                path: '/test4',
                name: 'Test4',
                component: Test4_FacultyAdminLoginNavigate
            },
            {
                path: '/test5',
                name: 'Test5',
                component: Test5_SA112View
            },
            /*
                管理员端
             */
            // 4-1 数据管理——模板管理
            {
                path: '/templateManagement',
                name: 'TemplateManagement',
                component: TemplateManagement
            },
            // 4-2 数据管理——论文元数据管理
            {
                path: '/splitPaperMetadataManagement',
                name: 'SplitPaperMetadataManagement',
                component:SplitPaperMetadataManagement
            },
            // 4-3 数据管理——抽取正文内容管理
            {
              path:'/extractContentManagement',
              name: 'ExtractContentManagement',
              component:ExtractContentManagement
            },
            // 4-4 数据管理——评分管理
            {
              path: '/factorScoreManagement',
              name: 'FactorScoreManagement',
              component:FactorScoreManagement
            },
            // 6统计分析
            {
                path: '/statisticAnalysis',
                name: 'StatisticAnalysis',
                component:StatisticAnalysis
            },
            // 7预测
            {
                path: '/predictionByKeywords',
                name: 'PredictionByKeywords',
                component: PredictionByKeywords
            }
        ]
    }
]

//创建路由
const router = createRouter({
    history: createWebHistory(),
    routes,
    // linkActiveClass: 'active'
});

// 路由守卫
router.beforeEach((to, from, next) => {
    const requiresAuth = to.matched.some(record => record.meta.requiresAuth);

    // 检查用户是否已登录
    // 用户登录后，token 存在 localStorage 中
    const isLoggedIn = localStorage.getItem('pinia-token');

    // 如果需要认证且用户未登录，则重定向到登录页面
    if (requiresAuth && !isLoggedIn) {
        next('/login');  // 跳转到登录页面
    } else {
        next();  // 继续导航
    }
});

export default router;